<template>
  <BasicTable @register="registerTable">
    <template #tableTitle>
      <a-button preIcon="ant-design:sync-outlined" type="primary" @click="handleSyncUser" style="margin-right: 5px">同步</a-button>
    </template>
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!--编辑抽屉-->
  <MpUserDrawer @register="registerDrawer" @success="reload" :showFooter="showFooter" />
</template>
<script lang="ts" name="weixin-mp-user" setup>
  import { ref, onMounted } from "vue"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useDrawer } from "/@/components/Drawer"
  import { useMessage } from "/@/hooks/web/useMessage"
  import MpUserDrawer from "./MpUserDrawer.vue"
  import { columns, searchFormSchema } from "./data"
  import { pageMpUser, syncMpUser } from "./api"
  import { useListPage } from "/@/hooks/system/useListPage"

  const { createMessage } = useMessage()
  const showFooter = ref(true)
  const [registerDrawer, { openDrawer }] = useDrawer()

  // 列表页面公共参数、方法
  const { prefixCls, tableContext } = useListPage({
    designScope: "weixin-mp-tag",
    tableProps: {
      title: "微信公众号粉丝列表",
      api: pageMpUser,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
      },
      bordered: false,
      actionColumn: {
        width: 90,
        title: "操作",
        dataIndex: "action",
        fixed: "right",
        slots: { customRender: "action" },
      },
    },
  })
  const [registerTable, { getForm, reload }, { rowSelection, selectedRowKeys }] = tableContext

  onMounted(() => {
    const { updateSchema } = getForm()
  })

  const handleSyncUser = () => {
    const { getFieldsValue } = getForm()
    createMessage.loading({
      content: `正在同步粉丝...`,
      key: "sync_mp_user",
      duration: 0,
    })
    syncMpUser({})
      .then(() => {
        reload()
        createMessage.success({
          content: `同步成功`,
          key: "sync_mp_user",
          duration: 2,
        })
      })
      .catch((e) => {
        createMessage.destroy("sync_mp_user")
      })
  }

  /**
   * 新增
   */
  function handleCreate() {
    const { getFieldsValue } = getForm()
    showFooter.value = true
    openDrawer(true, {
      isUpdate: false,
      accountId: getFieldsValue().accountId,
    })
  }

  /**
   * 编辑
   */
  function handleEdit(record: Recordable) {
    showFooter.value = true
    const { getFieldsValue } = getForm()
    openDrawer(true, {
      record,
      accountId: getFieldsValue().accountId,
      isUpdate: true,
    })
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: "编辑",
        onClick: handleEdit.bind(null, record),
      },
    ]
  }
</script>
